{% extends 'myadmin/index.html' %}

{% block title %}
<title>后台-商品分类列表</title>
{% endblock %}

{% block con %}
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title  am-cf">商品分类列表</div>
                </div>
                <div class="widget-body  am-fr">

                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                        <div class="am-form-group">
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <a href="{% url 'myadmin_cate_add' %}" class="am-btn am-btn-default am-btn-success"><span class="am-icon-plus"></span> 新增</a>
                                </div>
                            </div>
                        </div>
                    </div>

                    <form action="">
                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
                        <div class="am-form-group tpl-table-list-select">
                            <select name="types" data-am-selected="{btnSize: 'sm'}" style="display: none;">
                              <option value="all" {% if request.GET.types == 'all' %} selected {% endif %}>所有类别</option>
                            </select>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                        <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                            <input name='keywords'value=''type="text" class="am-form-field ">
                            <span class="am-input-group-btn">
                                <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search" ></button>
                              </span>
                        </div>
                    </div>
                    </form>

                    <div class="am-u-sm-12">
                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>分类名称</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% load pagetag %}
                                {% for v in categorylist %}
                                <tr>
                                    <td>{{ v.id }}</td>
                                    <td class="am-text-middle">
                                        <span>{% sj v.path %}</span>
                                        <span class="name">{{ v.name }}</span>
                                    </td>
                                    <td class="am-text-middle">
                                        <div class="tpl-table-black-operation">

                                            <a href="javascript:void(0)" class="categoryedit">
                                                <i class="am-icon-pencil  "></i> 编辑
                                            </a>
                                            <a href="javascript:void(0)" class="tpl-table-black-operation-del">
                                                <i class="am-icon-trash"></i> 删除
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                      
                                <!-- more data -->
                            </tbody>
                        </table>
                    </div>
                    <div class="am-u-lg-12 am-cf">

                        <div class="am-fr">
                            <ul class="am-pagination tpl-pagination">
                                {% load pagetag %}
                                {% showpages categorylist.paginator.num_pages request %}
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type='text/javascript' >
    // 删除按钮绑定单击事件
    $('.tpl-table-black-operation-del').click(function(){
        // 获取当前单击对象
        var a = $(this)
        // 获取当前删除对象id
        var did = a.parents('tr').find('td:eq(0)').text()
        // 发送ajax请求
        $.get('{% url "myadmin_cate_del" %}',{'id':did},function(data){
            // 删除成功
            if(data['error']==0){
                a.parents('tr').remove()
                alert(data['msg'])
            }else{
                // 删除失败 
                alert(data['msg'])
            }
        },'json')
    })

    // 编辑按钮绑定单击击事件
    $('.categoryedit').click(function(){
        // 获取当前单击对象
        var a = $(this)
        // 获取当前编辑对象id
        var id = a.parents('tr').find('td:eq(0)').text()
        // 获取当前编辑对象分类名
        var name = a.parents('tr').find('.name').text()
        // 动态创建input输入框 放在name位置
        var inp = $('<input value="'+name+'" style="color:#000">')     
        a.parents('tr').find('.name').html(inp)
        inp.select()
        inp.mousedown(function(){
            inp.blur(function(){
                // 获取修改后名称
                var ename = $(this).val()
                // 判断名称是否变化
                if(ename == name){
                    // 恢复原样
                    a.parents('tr').find('.name').html(name)  
                }else{
                    // 发送请求
                    $.get('{% url "myadmin_cate_edit" %}',{'id':id,'name':ename},function(data){
                        a.parents('tr').find('.name').html(ename) 
                        alert(data['msg'])
                    },'json')                   
                }
            })
        })
    })

    </script>

{% endblock %}

